package com.zxy.express.service.impl;

import com.autopai.base.common.mybatis.service.impl.BaseServiceImpl;
import com.autopai.base.common.web.constant.Constant;
import com.autopai.base.common.web.convert.ConvertUtils;
import com.autopai.base.common.web.page.PageData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxy.express.dao.AchievementDao;
import com.zxy.express.dto.AchievementDTO;
import com.zxy.express.entity.AchievementEntity;
import com.zxy.express.service.AchievementService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 成就勋章定义表
 *
 * @author zhangxiying ${email}
 * @since ${version} 2025-07-01
 */
@Service
public class AchievementServiceImpl extends BaseServiceImpl<AchievementDao, AchievementEntity> implements AchievementService {

    @Override
    public PageData<AchievementDTO> page(Map<String, Object> params) {
        IPage<AchievementEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );

        return getPageData(page, AchievementDTO.class);
    }

    @Override
    public List<AchievementDTO> list(Map<String, Object> params) {
        List<AchievementEntity> entityList = baseDao.selectList(getWrapper(params));

        return ConvertUtils.sourceToTarget(entityList, AchievementDTO.class);
    }

    private QueryWrapper<AchievementEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<AchievementEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public AchievementDTO get(String id) {
        AchievementEntity entity = baseDao.selectById(id);

        return ConvertUtils.sourceToTarget(entity, AchievementDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(AchievementDTO dto) {
        AchievementEntity entity = ConvertUtils.sourceToTarget(dto, AchievementEntity.class);

        insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AchievementDTO dto) {
        AchievementEntity entity = ConvertUtils.sourceToTarget(dto, AchievementEntity.class);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String[] ids) {
        //逻辑删除
        //logicDelete(ids, AchievementEntity.class);

        //物理删除
        baseDao.deleteBatchIds(Arrays.asList(ids));
    }

}